﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using BDSync.Domains.Entities;
using DBSync.Tools;
using LiteDB;

namespace BDSync.Domains.DBConn
{
    public class DBConnService : IDBConnService 
    {
        private DBConnRepo mRepo;
        
        public DBConnService( DBConnRepo repo)
        {
            mRepo = repo;
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public String Insert(DBConnObj entity)
        {
            var id = IdGeneratorHelper.Instance.GetId().ToString();
            entity.Id = id;
            return mRepo.Insert(entity).AsString;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public  bool Update(DBConnObj entity)
        {
            return mRepo.Update(entity);
        }


        /// <summary>
        /// 更新批量
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(IEnumerable<DBConnObj> entities)
        {
            foreach (var entity in entities)
            {
                if (string.IsNullOrEmpty(entity.Id))
                {
                    Insert(entity);
                }

                mRepo.Update(entity);
            }

            return true;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public  bool Delete(DBConnObj entity)
        {
            return mRepo.Delete(entity);
        }


        /// <summary>
        /// 查找全部
        /// </summary>
        /// <returns></returns>
        public  IEnumerable<DBConnObj> FindAll()
        {
            return mRepo.FindAll();
        }
    }
}
